  //
//  AppDelegate.swift
//  sinaweiboFirst
//
//  Created by mac on 16/3/5.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //设置全局颜色
        setThemeColor()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        
        //注册通知
        registerNotification()
        
        //设置根视图控制器
        defaultRootController()
        
        window?.makeKeyAndVisible()
        
        return true
    }

    private func defaultRootController() {
        let rootVC = UserAccount.shareAccount.userLogin ? WelcomeViewController() : MainViewController()
        window?.rootViewController = rootVC
    }
    
//*********通知的监听****************
    @objc private func changeRootVC(n: NSNotification) {
        print(n)
        
//        let rootVC = UINavigationController(rootViewController: ComposeViewController())
        
//         let rootVC = UINavigationController(rootViewController: PictureSelectedViewController())
//        
//        window?.rootViewController = rootVC
//        
        window?.rootViewController = (n.object == nil) ? MainViewController() : WelcomeViewController()
    }

//************注册通知**************
//object: 关注谁发通知  nil(广播)所有对象发送的通知都可以接收 不为nil(通知) changeRootVC后面带参数要加":"
    private func registerNotification() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeRootVC:", name: SwitchRootViewController, object: nil)
    }
    
    private func setThemeColor() {
        //设置导航栏的颜色 全局有效 
        //必须要在控件实例化之前设置
        UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor
    }
    
//***************移除通知**************
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
}

